<?php namespace App\Utils;
use Validator;
class CommonUtils {
    
	protected $uuidKey = 'ray201706060935';
    /* *
	* @description : 校验参数是否是用户名 0 都不是  1 email 2 phone
	* @param: $name， 用户名 email or phone
	* @return: 0 都不是  1 email 2 phone
	*/
	public function isEmailorPhone($name){
		//邮箱的验证
		$validator = Validator::make(array('email'=>$name),array('email'=> 'email'));
		if (!$validator->fails()) {
			return  1;
		} 
		//短信获取验证码
		//验证手机号码
		if(preg_match("/^1[3456789][0-9]{9}$/",$name)){
			 return 2;
		}
		return 0;
	}
	
	/* *
	* @description : 版本号比较 
	* @param: $va 
	* @param: $vb 
	* @return: 0 a=b , 1 a>b, -1 a<b
	*/
	public function compareVison($va, $vb) {
		$arr_av = explode('.', $va);
		$arr_vb = explode('.', $vb);
		foreach ($arr_av as $key => $value) {
			if (intval($value) > intval($arr_vb[$key])) {
				return 1;
			} else if (intval($value) < intval($arr_vb[$key])) {
				return -1;
			}
		}
		return 0;
	}
	
	/* *
	* @description : 异或加密  
	* @param: $str，$key  
	* @return:  $str
	*/
	public function xor_enc($str, $key='WelcomeToBeiJing') {
		$crytxt = '';
		$keylen = strlen($key);
		for ($i = 0; $i < strlen($str); $i++) {
			$k = $i % $keylen;
			$crytxt .= $str[$i] ^ $key[$k];
		}
		return $crytxt;
	}
     
    /* *
	* @description : 增则替换html内容
	* @param: $str 替换前
	* @return:  $str 替换后
	*/
    public function pregReplaceContent($str = ''){
        $str = preg_replace('/font-size\s*:.*?((px|em|rem|%)|(?=(;|"|\')))/i', '', $str);
        $str = preg_replace('/line-height\s*:.*?((px|em|rem|%)|(?=(;|"|\')))/i', '', $str);
        $str = str_replace('_ueditor_page_break_tag_', '', $str);
        $str = preg_replace('/(\<video[^>]*width.*?=.*?)[\"\'].*?[\"\']/', '\1"100%"', $str);
        $str = preg_replace('/(\<video[^>]*height.*?=.*?)[\"\'].*?[\"\']/', '\1"auto"', $str);
        $str = preg_replace('/(<video.*?)/','$1 style="max-height:5rem"', $str);
        $str = preg_replace('/(\<iframe[^>]*width.*?=\s*)[\"\'\S].*?[\"\'\s ]/', '\1"100%" ', $str);
        $str = preg_replace('/(\<embed[^>]*width.*?=\s*)[\"\'\S].*?[\"\'\s ]/', '\1"100%" ', $str);
//        $str = preg_replace('/(\<iframe[^>]*height.*?=.*?)[\"\'].*?[\"\']/', '\1"auto"', $str);
        $str = preg_replace('/(<iframe.*?)/','$1 style="max-height:5rem"', $str);
//        $str = preg_replace('/(\<img[^>]*width.*?=.*?)[\"\'].*?[\"\']/', '\1"100%"', $str);
        $str = preg_replace('/(<img.*?)/','$1 style="max-width:100%"',$str);
//        $str = preg_replace('/(<table.*?)/','$1 style="max-width:100%"',$str);
        $str = preg_replace('/(\<table[^>]*width.*?:\s*)\d+/', '\1 100%', $str);
        $str = preg_replace('/(\<table[^>]*100%.*?)px/', '\1 ', $str);
        $str = preg_replace('/(\<table[^>]*100%.*?)rem/', '\1 ', $str);
        $str = preg_replace('/(\<img[^>]*height.*?=.*?)[\"\'].*?[\"\']/', '\1"auto"', $str);
        $str = preg_replace('/(\<audio[^>]*width.*?=.*?)[\"\'].*?[\"\']/', '\1"100%"', $str);
        $str = preg_replace('/(\<audio[^>]*height.*?=.*?)[\"\'].*?[\"\']/', '\1"auto"', $str);
        return $str;
    }
    
    /* *
	* @description : 截取字符串之间的值
	* @param:  
	* @return:  
	*/
    public function getNeedBetween($kw,$mark1,$mark2 = null){
        $st =strrpos($kw,$mark1) + strlen($mark1)-1;
        $ed =strrpos($kw,$mark2);
        if($mark2 === null){
            $ed = strlen($kw);
        }
        if(($st==false||$ed==false)||$st>=$ed){
           return 0; 
        }
        $kw=substr($kw,($st+1),($ed-$st-1));
        return $kw;
    }
    
    /* *
	* @description : r
	* @param: $name， 用户名 email or phone
	* @return: 0 都不是  1 email 2 phone
	*/
	public function getUuid($id){
        return md5($this->uuidKey.md5($id)).$id;
	}
     /* *
	* @description : 校验参数是否是用户名 0 都不是  1 email 2 phone
	* @param: $name， 用户名 email or phone
	* @return: 0 都不是  1 email 2 phone
	*/
	public function getId($uuid){
		//截取后
        $id = substr($uuid, 32);
        if(md5($this->uuidKey.md5($id)) === substr($uuid,0,32)){
            return $id;
        }
        return ;
	}
}